#include "musicslider.h"
#include "ui_musicslider.h"
#include<QMouseEvent>
#include<QDebug>
MusicSlider::MusicSlider(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicSlider)
{
    ui->setupUi(this);
    currentpos = 0;
    maxwidth = width();
    movesilder();
}

MusicSlider::~MusicSlider()
{
    delete ui;
}

void MusicSlider::mouseMoveEvent(QMouseEvent *event)
{
    QRect rect=QRect(0,0,geometry().width(),geometry().height());
    QPoint pos=event->pos();
    if(!rect.contains(pos))
    {
        return;
    }
    if(event->buttons()==Qt::LeftButton)
    {
        currentpos=event->pos().x();
        if(currentpos<0)
        {
            currentpos=0;
        }
        if(currentpos>maxwidth)
        {
            currentpos=maxwidth;
        }
        movesilder();
    }
}

void MusicSlider::mousePressEvent(QMouseEvent *event)
{
    currentpos=event->pos().x();
    movesilder();
}

void MusicSlider::mouseReleaseEvent(QMouseEvent *event)
{
    qDebug()<<"-";
    currentpos=event->pos().x();
    movesilder();
    emit SetMusicSliderposition((float)currentpos/maxwidth);
}

void MusicSlider::setStep(float be)
{
    currentpos=maxwidth*be;
    movesilder();
}

void MusicSlider::movesilder()
{

    ui->outLine->setGeometry(ui->outLine->x(),ui->outLine->y(),currentpos,ui->outLine->height());
}
